package nl.scalasim
package actors
package citizenactivities

import scala.actors.{OutputChannel, Actor}
import nl.scalasim.actors.traits.SimulationActor

/**
 * Activity is an implementation of the GoF Activity Pattern.
 *
 * An Activity contains the required logic for specific activities of a
 * citizen; during its lifetime, the currentActivity of a citizen can shift,
 * hence this abstraction. 
 *
 * @author Yigal Duppen
 * @since Oct 28, 2010
 */
abstract class Activity extends Logging {

  def citizen: Citizen

  def activate: Unit

  def react = citizen.react _

  private var currentAckChannel: OutputChannel[Message] = _

  def handleArrived(target: SimulationActor) {
    log.warning("Received 'Arrived' at " + target + " while in currentActivity " + this);
  }

  def handleFound(target: SimulationActor) {
    log.warning("Received 'Found' " + target + " while in currentActivity " + this)
  }

  def handleAct: Unit

  def handleAct(replyTo: OutputChannel[Message]): Unit = {
    currentAckChannel = replyTo
    // todo: add done as a continuation
    handleAct
  }

  def done(): Unit = {
    currentAckChannel ! Ack
    currentAckChannel = null
  }

}

